package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.SearchTopicAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerSearchTopicComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.SearchTopicModule;
import com.zw.yzk.kabu.model.SearchTopicModel;
import com.zw.yzk.kabu.presenter.SearchTopicPresenter;
import com.zw.yzk.kabu.view.view.SearchTopicView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by zhanwei on 2017/2/5.
 */

public class SearchOrCreateTopicActivity extends BaseActivity implements SearchTopicView, SearchTopicAdapter.OnItemClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.result)
    RecyclerView result;

    @Inject
    SearchTopicPresenter presenter;
    private SearchTopicAdapter adapter;
    private boolean isSearchResult;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_topic;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {

    }

    @Override
    public void initComponent() {
        DaggerSearchTopicComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .searchTopicModule(new SearchTopicModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initRecyclerView();
        getSearchResult(0, "", true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.cancel)
    public void cancel() {
        finish();
    }

    @Override
    public void searchTopicSuccess(List<SearchTopicModel> list) {
        if (isSearchResult && !TextUtils.isEmpty(search.getText().toString())) {
            SearchTopicModel create = new SearchTopicModel();
            create.setTopicContent("#" + search.getText().toString() + "#");
            list.add(0, create);
        }
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.size() > 0 ? State.LOADING_SUCCEED : State.LOAD_ALL);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
    }

    @Override
    public void searchTopicFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    @Override
    public void onTopicItemClicked(SearchTopicModel model) {
        Intent intent = new Intent();
        intent.putExtra(ActivityConstant.PUBLISH_TOPIC, model.getTopicContent());
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnTextChanged(value = R.id.search, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        getSearchResult(0, s.toString(), true);
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyclerView() {
        result.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new SearchTopicAdapter(null);
        adapter.setOnItemClickedListener(this);
        result.setAdapter(adapter);

        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getSearchResult(0, search.getText().toString(), false);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getSearchResult(adapter.getItemCount(), search.getText().toString(), false);
                    }
                }).build();
    }

    /**
     * 获取搜索结果
     */
    private void getSearchResult(int start, String key, boolean search) {
        presenter.searchTopic(start, key);
        isSearchResult = search;
    }

}
